/**
 * jims
 */
package com.jims.master.modules.drug.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.drug.entity.DrugClassDict;
import com.jims.master.modules.drug.dao.DrugClassDictDao;

/**
 * 药品类别字典表Service
 * @author CTQ
 * @version 2017-03-01
 */
@Service
@Transactional(readOnly = true)
public class DrugClassDictService extends CrudService<DrugClassDictDao, DrugClassDict> {

	public DrugClassDict get(String id) {
		return super.get(id);
	}

	/**
	 * 验证药品类别唯一性
	 * @param drugClassDict
	 * @return
	 */
	public int validUnique(DrugClassDict drugClassDict) {
		if (drugClassDict.getParent()!=null&&drugClassDict.getParent().getId()!=null&&!drugClassDict.getParent().getId().equals("")){
			DrugClassDict dict = super.get(drugClassDict.getParent().getId());
			drugClassDict.setClassCode(dict.getClassCode()+drugClassDict.getClassCode());
		}
		List<DrugClassDict> list = dao.validUnique(drugClassDict);
		return list.size();
	}

	/**
	 * 根据父级ID查询
	 * @param parentId
	 * @return
	 */
	public int findByParentId(String parentId) {
		return dao.findByParentId(parentId);
	}
	
	@Transactional(readOnly = false)
	public int saveClassDict(DrugClassDict drugClassDict) {
		if (drugClassDict.getParent()!=null&&drugClassDict.getParent().getId()!=null&&!drugClassDict.getParent().getId().equals("")){
			DrugClassDict dict = super.get(drugClassDict.getParent().getId());
			drugClassDict.setClassCode(dict.getClassCode()+drugClassDict.getClassCode());
		}
		return super.save(drugClassDict);
	}

	@Transactional(readOnly = false)
	public int delete(DrugClassDict drugClassDict) {
		return super.delete(drugClassDict);
	}
	
}